package com.nrtech.brebes;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.webkit.WebView;
import android.widget.RatingBar;
import android.widget.TextView;

@SuppressLint("SetJavaScriptEnabled")
public class InfoActivity extends Activity implements RatingBar.OnRatingBarChangeListener {

	private WebView webView;
	private RatingBar infoRatingBar;
	private TextView helloTextView;
	private String mapID;
	private String BASE_URL = "http://nrtech.org/maps/rating.php?";
	
	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_info);
		
		Intent intent = getIntent();
		mapID = intent.getExtras().getString("mapid");
		
		//helloTextView = (TextView)findViewById(R.id.textView1);
		//helloTextView.setText(mapID);
		
		ClientWebView webClient = new ClientWebView();
		webView = (WebView) findViewById(R.id.webViewInfo);
		webView.setWebViewClient(webClient);
		webView.getSettings().setJavaScriptEnabled(true);
		webClient.shouldOverrideUrlLoading(webView, "http://nrtech.org/maps/info.php?id="+mapID);
		
		infoRatingBar = (RatingBar) findViewById(R.id.ratingBarInfo);
		((RatingBar)findViewById(R.id.ratingBarInfo)).setOnRatingBarChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_info, menu);
		return true;
	}	

	public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
        /*ClientWebView webClient = new ClientWebView();
		webView = (WebView) findViewById(R.id.webViewInfo);
		webView.setWebViewClient(webClient);
		webView.getSettings().setJavaScriptEnabled(true);
		webClient.shouldOverrideUrlLoading(webView, "http://nrtech.org/maps/rating.php?maps=" + mapID + "&rating=" + rating);*/
		//Intent intent = new Intent(this, FbActivity.class);		
		//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com")); 
		//startActivity(intent);
		// TODO Auto-generated method stub
		try {
			@SuppressWarnings("deprecation")
			String URL = BASE_URL + "maps=" + mapID 
					+"&rating=" + rating;
			System.out.println(URL);
			AsyncHttpClient client = new AsyncHttpClient();
			client.get(URL, new AsyncHttpResponseHandler() {
			    @Override
			    public void onSuccess(String response) {
			        System.out.println(BASE_URL);					        
			        System.out.println(response);	
			        callFbActivity();
			    }
			});	
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}
	
	private AlertDialog callAlert(String message) {
		AlertDialog builder = new AlertDialog.Builder(this).create();
    	builder.setTitle("Title");
    	builder.setMessage(message);
    	//builder.setPositiveButton("OK", null);
    	//AlertDialog dialog = builder.show();
    	builder.show();
    	return builder;
	}
	
	public boolean callFbActivity() {
		try {
			Intent intent = new Intent(this, FbActivity.class);		
			//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vogella.com")); 
			startActivity(intent);
		} catch(Exception ex) {
			System.out.println(ex.getMessage());
			return false;
		}
		return true;
	}
}
